package com.rightsidetech.http.controller.agentApp.trade;

import com.github.pagehelper.PageInfo;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.rightsidetech.commom.base.PageRequest;
import com.rightsidetech.commom.base.ResponseData;
import com.rightsidetech.commom.enums.CodeIdEnum;
import com.rightsidetech.commom.utils.BeanValidator;
import com.rightsidetech.http.aspect.Log;
import com.rightsidetech.http.aspect.Token;
import com.rightsidetech.http.base.BaseController;
import com.rightsidetech.system.dtos.response.store.TdCommodityAgentAppStoreListResp;
import com.rightsidetech.system.entity.store.SysStoreInfo;
import com.rightsidetech.system.service.SysAgentInfoService;
import com.rightsidetech.system.service.SysStoreInfoService;
import com.rightsidetech.trade.dtos.request.TdCommodityAgentAppRequest;
import com.rightsidetech.trade.dtos.request.TdCommodityOrderNoRequest;
import com.rightsidetech.trade.dtos.response.TdCommodityAgentAppResp;
import com.rightsidetech.trade.dtos.response.TdCommodityStoreAppListResp;
import com.rightsidetech.trade.dtos.response.TdCommodityStoreOrderDetailResp;
import com.rightsidetech.trade.service.TdCommodityUserOrderService;
import com.rightsidetech.user.service.UserInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shenyu.client.springmvc.annotation.ShenyuSpringMvcClient;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.ConstraintViolation;

@RestController
@RequestMapping("/agentApp/trade")
@ShenyuSpringMvcClient(path = "/agentApp/trade/**")
@Api(tags = "代理商app订单接口")
@Slf4j
public class TdCommodityAgentAppController extends BaseController {


    @Resource
    private TdCommodityUserOrderService tdCommodityUserOrderService;
    @Resource
    private SysAgentInfoService sysAgentInfoService;
    @Resource
    private UserInfoService userInfoService;
    @Resource
    private SysStoreInfoService sysStoreInfoService;

    @Log
    @Token
    @ApiOperation("门店订单统计")
    @GetMapping("/storeOrderStatis")
    public ResponseData<TdCommodityAgentAppResp> storeOrderStatis() {
        Long agentInfoId = sysAgentInfoService.findAgentInfoId(Long.parseLong(getAttribute()));
        return ResponseData.success(tdCommodityUserOrderService.storeOrderStatis(agentInfoId, null));
    }

    @Log
    @Token
    @ApiOperation("门店订单统计列表")
    @GetMapping("/storeOrderStatisPage")
    public ResponseData<PageInfo<TdCommodityAgentAppStoreListResp>> storeOrderStatisPage(PageRequest req) {
        ConstraintViolation validate = BeanValidator.validate(req);
        if (validate != null) {
            return new ResponseData<>(CodeIdEnum.ParameterMalformed.getValue(), validate.getMessage());
        }
        Long agentInfoId = sysAgentInfoService.findAgentInfoId(Long.parseLong(getAttribute()));
        PageInfo<TdCommodityAgentAppStoreListResp> pageInfo = sysStoreInfoService.storeOrderStatisPage(req, agentInfoId);
        if (!StringUtils.isEmpty(pageInfo) && !StringUtils.isEmpty(pageInfo.getList()) && pageInfo.getList().size() > 0) {
            for (TdCommodityAgentAppStoreListResp listResp : pageInfo.getList()) {
                TdCommodityAgentAppResp tdCommodityAgentAppResp = tdCommodityUserOrderService.storeOrderStatis(agentInfoId, listResp.getStoreId());
                listResp.setUnPay(tdCommodityAgentAppResp.getUnPay());
                listResp.setUnReceive(tdCommodityAgentAppResp.getUnReceive());
                listResp.setService(tdCommodityAgentAppResp.getService());
            }
        }
        return ResponseData.success(pageInfo);
    }


    @Log
    @Token
    @ApiOperation("门店订单列表")
    @GetMapping("/storeOrderPage")
    @ApiOperationSupport(ignoreParameters = {"agentId"})
    public ResponseData<PageInfo<TdCommodityStoreAppListResp>> storeOrderPage(TdCommodityAgentAppRequest req) throws Exception {
        ConstraintViolation validate = BeanValidator.validate(req);
        if (validate != null) {
            return new ResponseData<>(CodeIdEnum.ParameterMalformed.getValue(), validate.getMessage());
        }
        Long agentInfoId = sysAgentInfoService.findAgentInfoId(Long.parseLong(getAttribute()));
        req.setAgentId(agentInfoId);
        PageInfo<TdCommodityStoreAppListResp> pageInfo = tdCommodityUserOrderService.storeOrderPage(req);
        if (!StringUtils.isEmpty(pageInfo.getList()) && pageInfo.getList().size() > 0) {
            for (TdCommodityStoreAppListResp appListResp : pageInfo.getList()) {
                String headImg = userInfoService.getHeadImg(appListResp.getUserInfoId());
                appListResp.setHeadImg(headImg);
            }
        }
        return ResponseData.success(pageInfo);
    }


    @Log
    @Token
    @ApiOperation("订单详情")
    @GetMapping("/orderDetail")
    public ResponseData<TdCommodityStoreOrderDetailResp> findOrderDetail(TdCommodityOrderNoRequest req) {
        ConstraintViolation validate = BeanValidator.validate(req);
        if (validate != null) {
            return new ResponseData<>(CodeIdEnum.ParameterMalformed.getValue(), validate.getMessage());
        }

        Long agentInfoId = sysAgentInfoService.findAgentInfoId(Long.parseLong(getAttribute()));


        TdCommodityStoreOrderDetailResp detailResp = tdCommodityUserOrderService.findStoreOrderDetailByOrderNo(req.getOrderNo(), null, agentInfoId);
        if (!StringUtils.isEmpty(detailResp)) {
            String headImg = userInfoService.getHeadImg(detailResp.getUserInfoId());
            detailResp.setHeadImg(headImg);
            SysStoreInfo sysStoreInfo = sysStoreInfoService.selectByPrimaryKey(detailResp.getStoreId());
            if(!StringUtils.isEmpty(sysStoreInfo)){
                detailResp.setStoreName(sysStoreInfo.getStoreName());
                detailResp.setStoreImage(sysStoreInfo.getStoreImage());
                detailResp.setConnectWay(sysStoreInfo.getConnectWay());
            }
        } else {
            return ResponseData.fail(CodeIdEnum.NonOrder);
        }
        return ResponseData.success(detailResp);
    }

}
